package 多线程.银行取钱;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class GetMoney implements Runnable {
    Lock l = new ReentrantLock();

    Money m =  new Money();
    @Override
    public void run() {
//        l.lock();//上锁
        synchronized (this) {
            while (true) {
                if(m.getMoney()>=10){
                    m.setMoney(m.getMoney() - 10);
                    System.out.println(Thread.currentThread().getName() + "取走了10元");
                    System.out.println("剩余"+m.getMoney());
                }else {
                    System.out.println("没钱了");
                    break;
                }

//                l.unlock();//解锁
            }
        }

    }
    public synchronized void qu(){
        for (int i = 0; i < 50000; i++) {
            m.setMoney(m.getMoney() - 10);
            System.out.println(Thread.currentThread().getName() + "取走了10元");
            System.out.println("剩余"+m.getMoney());
        }

    }
}
